package com.shj.config.security;

import com.shj.entity.SysUser;
import com.shj.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 自定义成功处理器
 * @Author songhuajian
 * @Date 2020/1/11 22:00
 **/
@Component
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private SysUserService sysUserService;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        User user = (User) authentication.getPrincipal();
        request.getSession().setAttribute("userId", user.getUsername());

        SysUser sysUser = sysUserService.findByUserId(user.getUsername());
        request.getSession().setAttribute("agenCode", sysUser.getAgenCode());

        // 跳转页面
        String url = "/jsp/index.jsp";
        getRedirectStrategy().sendRedirect(request, response, url);
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
